Skip to content

fix[describeClassComponentFrame]: invoke constructor with new keyword#36455

Merged
hoxyq merged 1 commit into
mainfrom
component-stack-frame/dont-invoke-ctor-without-new
May 13, 2026
Merged

fix[describeClassComponentFrame]: invoke constructor with new keyword#36455
hoxyq merged 1 commit into
mainfrom
component-stack-frame/dont-invoke-ctor-without-new

Conversation

@hoxyq
Copy link
Copy Markdown
Contributor

@hoxyq hoxyq commented May 12, 2026

For JavaScript runtimes that do not have Reflect supported, we had a fall back that was calling the constructor with overridden this context via

fn.apply(Fake.prototype);

In ES6, it is required to call constructor only with the new keyword, otherwise the runtime is expected to throw a corresponding TypeError:

TypeError: Class constructor <> cannot be invoked without 'new'

We've observed this error in Hermes runtime, but this is applicable to V8 or any other runtime. The only reason why V8 wasn't affected is because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling new fn(), but with a temporary patched prototype of the class, which would make a trap out of the setter for props object. We use the same approach when Reflect APIs are available, but instead of modifying the prototype, we pass the fake context:

const Fake = function () {
throw Error();
};
// $FlowFixMe[prop-missing]
Object.defineProperty(Fake.prototype, 'props', {
set: function () {
// We use a throwing setter instead of frozen or non-writable props
// because that won't throw in a non-strict mode function.
throw Error();
},
});
if (typeof Reflect === 'object' && Reflect.construct) {
// We construct a different control for this case to include any extra
// frames added by the construct call.
try {
Reflect.construct(Fake, []);
} catch (x) {
control = x;
}
Reflect.construct(fn, [], Fake);


See tests implemented. Without the changes, the test would fail with the TypeError mentioned above.

@hoxyq hoxyq requested review from eps1lon and javache May 12, 2026 16:32
@meta-cla meta-cla Bot added the CLA Signed label May 12, 2026
@react-sizebot
Copy link
Copy Markdown

react-sizebot commented May 12, 2026

Comparing: d5736f0...929eaad

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.84 kB 6.84 kB = 1.88 kB 1.88 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.11% 613.53 kB 614.17 kB +0.07% 108.44 kB 108.52 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.84 kB 6.84 kB = 1.88 kB 1.88 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.10% 679.46 kB 680.11 kB +0.07% 119.40 kB 119.48 kB
facebook-www/ReactDOM-prod.classic.js +0.09% 699.88 kB 700.53 kB +0.07% 122.96 kB 123.05 kB
facebook-www/ReactDOM-prod.modern.js +0.09% 690.20 kB 690.84 kB +0.07% 121.35 kB 121.44 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-server/cjs/react-server.production.js +0.47% 145.64 kB 146.32 kB +0.37% 25.53 kB 25.63 kB
oss-stable/react-server/cjs/react-server.production.js +0.47% 145.64 kB 146.32 kB +0.37% 25.53 kB 25.63 kB
oss-experimental/react-server/cjs/react-server.production.js +0.46% 149.12 kB 149.81 kB +0.35% 26.32 kB 26.41 kB
oss-stable-semver/react-server/cjs/react-server.development.js +0.40% 209.92 kB 210.76 kB +0.38% 37.03 kB 37.17 kB
oss-stable/react-server/cjs/react-server.development.js +0.40% 209.92 kB 210.76 kB +0.38% 37.03 kB 37.17 kB
oss-experimental/react-server/cjs/react-server.development.js +0.39% 214.71 kB 215.55 kB +0.36% 37.95 kB 38.08 kB
oss-experimental/react-markup/cjs/react-markup.production.js +0.26% 246.89 kB 247.53 kB +0.17% 45.76 kB 45.84 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.26% 250.05 kB 250.70 kB +0.21% 45.08 kB 45.18 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.26% 250.07 kB 250.72 kB +0.20% 45.11 kB 45.20 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.js +0.25% 254.97 kB 255.62 kB +0.19% 46.96 kB 47.04 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.js +0.25% 255.00 kB 255.64 kB +0.18% 46.98 kB 47.06 kB
facebook-www/ReactDOMServer-prod.modern.js +0.25% 256.62 kB 257.27 kB +0.19% 45.88 kB 45.97 kB
facebook-www/ReactDOMServer-prod.classic.js +0.25% 258.96 kB 259.61 kB +0.18% 46.23 kB 46.32 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.25% 259.84 kB 260.48 kB +0.19% 46.51 kB 46.60 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.js +0.24% 265.33 kB 265.98 kB +0.18% 48.61 kB 48.69 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +0.24% 266.63 kB 267.27 kB +0.19% 48.75 kB 48.84 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.js +0.23% 277.68 kB 278.33 kB +0.20% 49.43 kB 49.52 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.js +0.23% 277.76 kB 278.40 kB +0.20% 49.45 kB 49.55 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.js +0.23% 278.44 kB 279.08 kB +0.17% 49.08 kB 49.16 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.js +0.23% 278.51 kB 279.16 kB +0.17% 49.10 kB 49.18 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.js +0.23% 283.30 kB 283.95 kB +0.16% 51.52 kB 51.61 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.js +0.23% 283.38 kB 284.02 kB +0.16% 51.55 kB 51.63 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.js +0.22% 289.10 kB 289.74 kB +0.17% 51.07 kB 51.15 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.js +0.22% 289.70 kB 290.35 kB +0.16% 50.86 kB 50.94 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.js +0.22% 291.37 kB 292.01 kB +0.16% 51.38 kB 51.46 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.js +0.22% 291.44 kB 292.09 kB +0.15% 51.41 kB 51.49 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.js +0.22% 295.39 kB 296.04 kB +0.15% 53.41 kB 53.49 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.js +0.21% 303.11 kB 303.76 kB +0.15% 53.26 kB 53.34 kB
oss-experimental/react-markup/cjs/react-markup.development.js +0.21% 395.88 kB 396.71 kB +0.13% 71.88 kB 71.97 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js +0.20% 408.83 kB 409.66 kB +0.13% 73.97 kB 74.06 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.20% 408.83 kB 409.66 kB +0.13% 73.97 kB 74.06 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js +0.20% 408.85 kB 409.69 kB +0.12% 74.00 kB 74.09 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.20% 408.85 kB 409.69 kB +0.12% 74.00 kB 74.09 kB

Generated by 🚫 dangerJS against 929eaad

@hoxyq hoxyq force-pushed the component-stack-frame/dont-invoke-ctor-without-new branch from 87b4bcb to b548fff Compare May 12, 2026 16:40
@hoxyq hoxyq force-pushed the component-stack-frame/dont-invoke-ctor-without-new branch from b548fff to 929eaad Compare May 12, 2026 16:44
@hoxyq hoxyq merged commit 8fc5763 into main May 13, 2026
240 checks passed
@hoxyq hoxyq deleted the component-stack-frame/dont-invoke-ctor-without-new branch May 13, 2026 21:05
github-actions Bot pushed a commit that referenced this pull request May 13, 2026
…#36455)

For JavaScript runtimes that do not have
[`Reflect`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
supported, we had a fall back that was calling the constructor with
overridden `this` context via
```
fn.apply(Fake.prototype);
```

In ES6, it is required to call constructor only with the `new` keyword,
otherwise the runtime is expected to throw a corresponding TypeError:
```
TypeError: Class constructor <> cannot be invoked without 'new'
```

We've observed this error in Hermes runtime, but this is applicable to
V8 or any other runtime. The only reason why V8 wasn't affected is
because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling `new fn()`,
but with a temporary patched prototype of the class, which would make a
trap out of the setter for `props` object. We use the same approach when
`Reflect` APIs are available, but instead of modifying the prototype, we
pass the fake context:

https://github.com/facebook/react/blob/d5736f098edee62c44f27b053e6e48f5fa443803/packages/shared/ReactComponentStackFrame.js#L129-L148

---

See tests implemented. Without the changes, the test would fail with the
`TypeError` mentioned above.

DiffTrain build for [8fc5763](8fc5763)
github-actions Bot pushed a commit that referenced this pull request May 13, 2026
…#36455)

For JavaScript runtimes that do not have
[`Reflect`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
supported, we had a fall back that was calling the constructor with
overridden `this` context via
```
fn.apply(Fake.prototype);
```

In ES6, it is required to call constructor only with the `new` keyword,
otherwise the runtime is expected to throw a corresponding TypeError:
```
TypeError: Class constructor <> cannot be invoked without 'new'
```

We've observed this error in Hermes runtime, but this is applicable to
V8 or any other runtime. The only reason why V8 wasn't affected is
because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling `new fn()`,
but with a temporary patched prototype of the class, which would make a
trap out of the setter for `props` object. We use the same approach when
`Reflect` APIs are available, but instead of modifying the prototype, we
pass the fake context:

https://github.com/facebook/react/blob/d5736f098edee62c44f27b053e6e48f5fa443803/packages/shared/ReactComponentStackFrame.js#L129-L148

---

See tests implemented. Without the changes, the test would fail with the
`TypeError` mentioned above.

DiffTrain build for [8fc5763](8fc5763)
github-actions Bot pushed a commit to code/lib-react that referenced this pull request May 17, 2026
…facebook#36455)

For JavaScript runtimes that do not have
[`Reflect`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
supported, we had a fall back that was calling the constructor with
overridden `this` context via
```
fn.apply(Fake.prototype);
```

In ES6, it is required to call constructor only with the `new` keyword,
otherwise the runtime is expected to throw a corresponding TypeError:
```
TypeError: Class constructor <> cannot be invoked without 'new'
```

We've observed this error in Hermes runtime, but this is applicable to
V8 or any other runtime. The only reason why V8 wasn't affected is
because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling `new fn()`,
but with a temporary patched prototype of the class, which would make a
trap out of the setter for `props` object. We use the same approach when
`Reflect` APIs are available, but instead of modifying the prototype, we
pass the fake context:

https://github.com/facebook/react/blob/d5736f098edee62c44f27b053e6e48f5fa443803/packages/shared/ReactComponentStackFrame.js#L129-L148

---

See tests implemented. Without the changes, the test would fail with the
`TypeError` mentioned above.

DiffTrain build for [8fc5763](facebook@8fc5763)
github-actions Bot pushed a commit to code/lib-react that referenced this pull request May 17, 2026
…facebook#36455)

For JavaScript runtimes that do not have
[`Reflect`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
supported, we had a fall back that was calling the constructor with
overridden `this` context via
```
fn.apply(Fake.prototype);
```

In ES6, it is required to call constructor only with the `new` keyword,
otherwise the runtime is expected to throw a corresponding TypeError:
```
TypeError: Class constructor <> cannot be invoked without 'new'
```

We've observed this error in Hermes runtime, but this is applicable to
V8 or any other runtime. The only reason why V8 wasn't affected is
because it implemented Reflect APIs.

Instead of the incorrect call, we will fall back to calling `new fn()`,
but with a temporary patched prototype of the class, which would make a
trap out of the setter for `props` object. We use the same approach when
`Reflect` APIs are available, but instead of modifying the prototype, we
pass the fake context:

https://github.com/facebook/react/blob/d5736f098edee62c44f27b053e6e48f5fa443803/packages/shared/ReactComponentStackFrame.js#L129-L148

---

See tests implemented. Without the changes, the test would fail with the
`TypeError` mentioned above.

DiffTrain build for [8fc5763](facebook@8fc5763)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants